package com.iam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iam.aspect.PerformanceAnno;
import com.iam.entity.User;
import com.iam.service.IUserService;
import com.iam.util.CommonResult;
import com.iam.util.QueryPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    // 还是使用驼峰
    @PostMapping("/listPage")
    public CommonResult list(@RequestBody QueryPage<User> queryBean) {
        // 传入的参数
        QueryWrapper<User> wrapper = new QueryWrapper<>(queryBean.getQuery());
        userService.list(wrapper);
        Page<User> page = new Page<>();
        page.setSize(queryBean.getSize());
        page.setCurrent(queryBean.getCurrent());
        // 自带
        IPage<User> userPage = userService.page(page, wrapper);
        return CommonResult.success(userPage);
    }

    @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
    @PerformanceAnno()
    public CommonResult getById(@PathVariable("id") String id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User user = userService.getById(id);
        return CommonResult.success(user);
    }

    @RequestMapping(value = "/username/{username}", method = RequestMethod.GET)
    @PerformanceAnno()
    public CommonResult getByUsername(@PathVariable("username") String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<User> users = userService.list(wrapper);
        return CommonResult.success(users);
    }

}

